<?php
/**
 * Created by PhpStorm.
 * User: surface
 * Date: 2019/1/11
 * Time: 20:07
 */

namespace app\Manage\controller;
use think\Db;
use think\Request;
use app\common\controller\Manage;

class BookingSalse extends Manage{

    public function index(Request $request)
    {
        if($request->get()) {
            $start_time = strtotime($request->get('start_date').' 00:00:00');
            $end_time = strtotime($request->get('end_date').' 23:59:59');
        } else {
            $hour=date("G");
            if($hour < 3){
                $start_time=strtotime(date("Y-m-d",strtotime("-1 day"))." 0:0:0");
                $end_time=strtotime(date("Y-m-d",strtotime("-1 day"))." 23:59:59");
            }else{
                list($start_time, $end_time) = \think\helper\Time::today();
            }
        }
        $field = ['SUM(taocan.price)' => 'booking_money', 'taocan.room_bn' => 'room_bn',  'room.name' => 'name', 'taocan.pay_status' => 'pay_status', 'taocan.ctime' => 'ctime'];
        $condition = [];
        $condition[] = ['pay_status', '=', 1];
        $condition[] = ['ctime', 'between', [$start_time, $end_time]];
        $list = Db::name('taocan_order')
                ->alias('taocan')
                ->join('room', 'room.bn = taocan.room_bn')
                ->where($condition)
                ->group('room_bn')
                ->field($field)
                ->select();
        //print_r($list);exit;
        $room_bn_arr = array_column($list, 'room_bn');
        $order_condition = [];
        $order_condition[] = ['ctime', 'between', [$start_time, $end_time]];
        $order_condition[] = ['room_bn', 'in', $room_bn_arr];
        $order_condition[] = ['status', '=', 2];
        $order_field = ['SUM(order_amount)' => 'total_amount', 'room_bn'];
        $order_list = Db::name('order')
            ->where($order_condition)
            ->group('room_bn')
            ->field($order_field)
            ->select();
        $order_list = arr_key($order_list, 'room_bn');
        //买单
        $bill_condition = [
            ['ctime', 'between', [$start_time, $end_time]],
            ['room_bn', 'in', $room_bn_arr],
            ['pay_status', '=', '2']    
        ];
        $pay_bill = Db::name('pay_bill')
                ->where($bill_condition)
                ->group('room_bn')
                ->field(['SUM(amount)' => 'total_amount', 'room_bn'])
                ->select();
        $pay_bill = arr_key($pay_bill, 'room_bn');
        
        foreach ($list as &$info) {
            $total_amount = $order_list[$info['room_bn']]['total_amount']? $order_list[$info['room_bn']]['total_amount']: 0;
            $bill_amount = $pay_bill[$info['room_bn']]['total_amount']? $pay_bill[$info['room_bn']]['total_amount']: 0;
            $info['total_amount'] = $total_amount;
            $info['bill_amount'] = $bill_amount;
            $info['total'] = $info['booking_money']+$total_amount+$bill_amount;
        }
        $this->assign('start_date', date('Y-m-d', $start_time));
        $this->assign('end_date', date('Y-m-d', $end_time));
        $this->assign('list', $list);
        return $this->fetch('index');
    }
    
    public function SalseView(Request $request) 
    {
       
        $room_bn = $request->get('room_bn');
        $start_time = strtotime($request->get('start_date').' 00:00:00');
        $end_time = strtotime($request->get('end_date').' 23:59:59');
        $book_condition = [];
        $book_condition[] = ['bn', '=', $room_bn];
        $book_condition[] = ['ctime', 'between', [$start_time, $end_time]];
        $room_field = ["FROM_UNIXTIME(ctime, '%Y-%m-%d')" => 'days','COUNT(*)' => 'num' ,'SUM(price)' => 'booking_money', 'room_id', 'ctime', 'bn'];
        $room_list = Db::name('room_booking')
                        ->where($book_condition)
                        ->field($room_field)
                        ->order('days desc')
                        ->group('days')
                        ->select();
        $room_list = arr_key($room_list, 'days');
        //聚合单点订单
        $order_condition = [];
        $order_condition[] = ['ctime', 'between', [$start_time, $end_time]];
        $order_condition[] = ['room_bn', '=', $room_bn];
        $order_condition[] = ['status', '=', 2];
        $order_field = ['SUM(order_amount)' => 'total_amount', "FROM_UNIXTIME(ctime, '%Y-%m-%d')" => 'days','room_bn'];
        $order_model = new \app\common\model\Order();
        $order_group = $order_model
            ->where($order_condition)
            ->group('days')
            ->field($order_field)
            ->select();
        //
        $order_group = arr_key($order_group, 'days');
        $field = ["FROM_UNIXTIME(ctime, '%Y-%m-%d')" => 'days','order_amount' ,'order_id', 'room_bn', 'user_id'];
        $order_list = $order_model::with('items')->field($field)->where($order_condition)->select();
        $user_o = array_column(json_decode(json_encode($order_list), true), 'user_id');
        $order_list = arrs_key($order_list, 'days');
        //套餐聚合
        $field = ['SUM(price)' => 'total_price', 'room_bn', 'taocan', "FROM_UNIXTIME(ctime, '%Y-%m-%d')" => 'days'];
        $condition = [];
        $condition[] = ['room_bn', '=', $room_bn];
        $condition[] = ['pay_status', '=', 1];
        $condition[] = ['ctime', 'between', [$start_time, $end_time]];
        $taocn_group = Db::name('taocan_order')
                ->where($condition)
                ->group('days')
                ->field($field)
                ->select();
        $taocn_group = arr_key($taocn_group, 'days');
        
        $taocn_field = '*, FROM_UNIXTIME(ctime, \'%Y-%m-%d\') as days';
        $taocn_list =Db::name('taocan_order') ->where($condition)->field($taocn_field)->select();
        $user_t = array_column(json_decode(json_encode($taocn_list),true), 'user_id');
        $taocn_list = arrs_key($taocn_list, 'days');
        //买单聚合
        $field = ['SUM(amount)' => 'total_amount', 'room_bn', "FROM_UNIXTIME(ctime, '%Y-%m-%d')" => 'days'];
        $condition = [];
        $condition[] = ['room_bn', '=', $room_bn];
        $condition[] = ['pay_status', '=', 2];
        $condition[] = ['ctime', 'between', [$start_time, $end_time]];
        $bill_group = Db::name('pay_bill')
                ->where($condition)
                ->group('days')
                ->field($field)
                ->select();
        $bill_group = arr_key($bill_group, 'days');
        
        $bill_field = '*, FROM_UNIXTIME(ctime, \'%Y-%m-%d\') as days';
        $bill_list =Db::name('pay_bill') ->where($condition)->field($bill_field)->select();
        $user_b = array_column(json_decode(json_encode($bill_list),true), 'user_id');
        $bill_list = arrs_key($bill_list, 'days');
        //用户
        $user = array_unique(array_merge($user_o, $user_t, $user_b));
        $user_list = Db::name('user')->where('id', 'in', $user)->select();
        $user_list = arr_key($user_list, 'id');
        //获取时间
        $day_num = ceil(($end_time-$start_time)/86400);
        $data = [];
        for($i=0;  $day_num > $i; $day_num--) {
            $days = date('Y-m-d', $start_time+($day_num-1)*86400);
            $data[$days]['room'] =  $room_list[$days];
            $data[$days]['order_group'] =  $order_group[$days];
            $data[$days]['order_list'] =  $order_list[$days];
            $data[$days]['taocn_group'] =  $taocn_group[$days];
            if(is_array($taocn_list[$days])) {
                foreach ($taocn_list[$days] as &$taocan_info) {
                    $taocan_info['taocan'] = json_decode($taocan_info['taocan'], true);
                }
            }
            $data[$days]['taocn_list'] =  $taocn_list[$days];
            
            $data[$days]['bill_group'] =  $bill_group[$days];
            $data[$days]['bill_list'] =  $bill_list[$days];
        }
        // print_r(json_encode($data));exit;
        //print_r(json_encode($data));exit;
        /*foreach ($room_list as &$room_info) {
            $condition = [
                ['room_bn', '=', $room_info['bn']],
                ['ctime', 'between', [strtotime($room_info['days'].' 00:00:00'), strtotime($room_info['days'].' 23:59:59')]]
            ];
            $room_info['goods'] = Db::name('taocan_order')->where($condition)->select();
            $room_info['goods'] = Db::name('taocan_order')->where($condition)->select();
        }*/
        //print_r($room_info);exit;
        $this->assign('user_list', $user_list);
        $this->assign('get', $request->get());
        $this->assign('data', $data);
        return $this->fetch('view');
    }
}